package com.braveman.im.ui.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.UIUtils;
import com.braveman.im.R;


public class VoiceBroadcastKitActivity extends BaseActivity {

    private RelativeLayout mSelfMotionPermit;
    private RelativeLayout mBackgrounderPermit;
    private TextView mTvSelfMotionPermitOne;
    private TextView mTvSelfMotionPermitTwo;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setLeftBtnIcon(R.mipmap.back)//
                .setTitleText("播报锦囊");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_voice_broadcast_kit;
    }

    @Override
    protected void initChildView() {
        mSelfMotionPermit = (RelativeLayout) findViewById(R.id.rl_self_motion_permit_one);
        mBackgrounderPermit = (RelativeLayout) findViewById(R.id.rl_self_motion_permit_two);
        mTvSelfMotionPermitOne = (TextView) findViewById(R.id.tv_self_motion_permit);
        mTvSelfMotionPermitTwo = (TextView) findViewById(R.id.tv_self_motion_permit_two);
    }

    @Override
    protected void initChildData() {
        Build build = new Build();
        Log.e("--------------", build.MANUFACTURER + "");
        mSelfMotionPermit.setVisibility(View.VISIBLE);
        mBackgrounderPermit.setVisibility(View.VISIBLE);
        switch (build.MANUFACTURER) {
            case "HUAWEI":
                mTvSelfMotionPermitOne.setText("允许开启自启动");
                mTvSelfMotionPermitTwo.setText("保护后台运行");
                break;
            case "Xiaomi":
                mTvSelfMotionPermitOne.setText("允许开启自启动模式");
                mTvSelfMotionPermitTwo.setText("允许关闭神隐模式");
                break;
            case "OPPO":
                mTvSelfMotionPermitOne.setText("允许开启自启动");
                mTvSelfMotionPermitTwo.setText("保护后台运行");
                break;
            case "samsung":
                mSelfMotionPermit.setVisibility(View.GONE);
                mBackgrounderPermit.setVisibility(View.GONE);
                break;
            default:
                mTvSelfMotionPermitOne.setText("允许开启自启动");
                mTvSelfMotionPermitTwo.setText("保护后台运行");
                break;
        }
    }

    @Override
    protected void setChildViewListener() {
        mSelfMotionPermit.setOnClickListener(this);
        mBackgrounderPermit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

            case R.id.rl_self_motion_permit_one:
                Build build = new Build();
                Log.e("--------------", build.MANUFACTURER + "");
                Intent intent = null;
                switch (build.MANUFACTURER) {
                    case "HUAWEI":
                        goProtect();
                        break;
                    case "Xiaomi":
                        try {
                            Intent i = new Intent();
                            ComponentName comp = new ComponentName("com.android.settings",
                                    "com.android.settings.BackgroundApplicationsManager");
                            i.setComponent(comp);
                            startActivity(i);
                        } catch (Exception e) {
                            UIUtils.showToast("该手机暂无此功能");
                        }
                        break;
                    case "OPPO":
                        intent = new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                        startActivity(intent);
                        break;
                    case "samsung":
                        intent = new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                        startActivity(intent);
                        break;
                    default:
                        intent = new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                        startActivity(intent);
                        break;
                }
                break;
            case R.id.rl_self_motion_permit_two:
                Build build1 = new Build();
                switch (build1.MANUFACTURER) {
                    case "HUAWEI":
                        UIUtils.showToast("该手机暂无此功能");
                        break;
                    case "Xiaomi":
                        try {
                            Intent i = new Intent(Intent.ACTION_MAIN);
                            i.addCategory(Intent.CATEGORY_LAUNCHER);
                            i.setComponent(new ComponentName("com.miui.powerkeeper", "com.miui.powerkeeper.ui.HiddenAppsContainerManagementActivity"));
                            startActivity(i);
                        } catch (Exception e) {
                            UIUtils.showToast("该手机暂无此功能");
                        }
                        break;
                    case "OPPO":
                        UIUtils.showToast("该手机暂无此功能");
                        break;
                    case "samsung":
                        UIUtils.showToast("该手机暂无此功能");
                        break;
                    default:
                        UIUtils.showToast("该手机暂无此功能");
                        break;
                }
                break;
        }
    }

    private Intent getAppDetailSettingIntent(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.BATTERY_SAVER_SETTINGS");
            localIntent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        return localIntent;
    }

    private void goProtect() {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");
            intent.setComponent(comp);
            startActivity(intent);
        } catch (Exception e) {
//            goMainager();
            e.printStackTrace();
        }
    }
}
